package com.gydz.gydzbaseframeworkboot.common.config;

import com.gydz.gydzbaseframeworkboot.common.utils.PasswordEncoder;

import com.gydz.gydzbaseframeworkboot.gsCenter.service.impl.UserDetailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import org.springframework.stereotype.Component;
@Component
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    LoginFailureHandler loginFailureHandler;

    @Autowired
    LoginSuccessHandler loginSuccessHandler;

    @Autowired
    MyLogoutSuccessHandler logoutSuccessHandler;

    @Autowired
    UserDetailServiceImpl userDetailService;




    private static final String[] URL_WHITELIST = {
            "/api/login",
            "/api/logout",
            "/login",
//            "/static/**",
            "/css/**",
            "/js/**",
            "/img/**",
//            "/layuimini/**",
            "/utils/**",
//            "/vue/**",
//            "/element-ui/**"
    };

    @Override
    public void configure(WebSecurity web) throws Exception {
//        web.ignoring().antMatchers("/css/**","/js/**","/img/**",
//                "/layuimini/**","/utils/**","/vue/**","/element-ui/**");
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable()
                // 登录配置
                .formLogin()
                .loginPage("/login")
                .usernameParameter("userAccount")
                .passwordParameter("password")
                .loginProcessingUrl("/api/login")

                .successHandler(loginSuccessHandler)
                .failureHandler(loginFailureHandler)

                .and()
                .logout()
                .logoutUrl("/api/logout")
                .logoutSuccessHandler(logoutSuccessHandler)

                // 禁用session
//                .and()
//                .sessionManagement()
//                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                // 配置拦截规则
                .and()
                .authorizeRequests()
                .antMatchers(URL_WHITELIST).permitAll()
                .anyRequest().authenticated()

                .and()
                .headers().frameOptions().sameOrigin();

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailService).passwordEncoder(new PasswordEncoder());
    }
}
